package com.wenjiang.service.impl;

import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenjiang.common.dto.ChangeRoleParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.common.vo.RoleDetailVo;
import com.wenjiang.entity.MallRole;
import com.wenjiang.mapper.MallMenuMapper;
import com.wenjiang.mapper.MallPermissionMapper;
import com.wenjiang.mapper.MallRoleMapper;
import com.wenjiang.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl <MallRoleMapper, MallRole> implements RoleService {

    @Autowired
    MallRoleMapper mallRoleMapper;

    @Autowired
    MallMenuMapper mallMenuMapper;

    @Autowired
    MallPermissionMapper mallPermissionMapper;



    @Override
    public Result getRole() {

        //获取全部角色
        List<MallRole> roles = baseMapper.selectList(null);
        return Result.succ(roles);

    }

    @Override
    public Result changRole(ChangeRoleParam changeRoleParam) {
        //判断修改是不是管理员
        MallRole beforeRole = mallRoleMapper.selectById(changeRoleParam.getBeforecheckRole());

        if(beforeRole.getRoleName().equals("admin")){
            return Result.fail("不能降级管理员角色");
        }
        mallRoleMapper.changeRole(changeRoleParam.getUserId(), changeRoleParam.getCheckRole());


        return Result.succ("修改成功");
    }

    @Override
    public Result getRoleDetail() {
        List<RoleDetailVo> roleDetail = mallRoleMapper.getRoleDetail();

        for (RoleDetailVo item:roleDetail){
            //查找对应的菜单
            List<Integer> menu = mallMenuMapper.getRoleMenu(item.getRoleId());
            // 查找对应的权限
            List<Integer> perm = mallPermissionMapper.getRolePermIds(item.getRoleId());
            item.setMenuIds(menu);
            item.setPermIds(perm);
        }

        return Result.succ(roleDetail);
    }
}
